# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import datetime

import pymongo

from ChineseLottery.items import WinOrLoseItem, PointItem, HalfItem, BasketballWinOrLose, BasketballPoint, \
    TotalBallItem, FootballResult, BasketballResult, LotteryNews, DSFootball


class SportMongoDBPipeline(object):
    def __init__(self):
        # 连接数据库
        self.client = pymongo.MongoClient(host='127.0.0.1', port=27017)
        self.db = self.client['Lottery']
        # 胜负彩集合
        self.win_or_lose_collection = self.db['FootballWinOrLose']
        self.win_or_lose_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 比分集合
        self.point_collection = self.db['FootballPoint']
        self.point_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 半全场集合
        self.half_collection = self.db['FootballHalf']
        self.half_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 总进球数集合
        self.total_ball_collection = self.db['FootballTotal']
        self.total_ball_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 篮球比分
        self.basketball_win_or_lose_collection = self.db['BasketballWinOrLose']
        self.basketball_win_or_lose_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 篮球大小分
        self.basketball_point_collection = self.db['BasketballPoint']
        self.basketball_point_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 足球结果
        self.football_result_collection = self.db['FootballResult']
        self.football_result_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 篮球结果
        self.basketball_result_collection = self.db['BasketballResult']
        self.basketball_result_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 资讯
        self.news_collection = self.db['News']
        self.news_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)
        # 外围数据
        self.football_matches_collection = self.db['FootballMatches']
        self.football_matches_collection.create_index([("timeStamp", pymongo.ASCENDING)], expireAfterSeconds=60)


    def process_item(self, item, spider):
        if(isinstance(item, WinOrLoseItem)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.win_or_lose_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, PointItem)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.point_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, HalfItem)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.half_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, BasketballWinOrLose)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.basketball_win_or_lose_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, BasketballPoint)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.basketball_point_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, TotalBallItem)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.total_ball_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, FootballResult)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.football_result_collection.insert_one(dict_item)
            return item
        elif (isinstance(item, BasketballResult)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.basketball_result_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, LotteryNews)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.news_collection.insert_one(dict_item)
            return item
        elif(isinstance(item, DSFootball)):
            dict_item = dict(item)
            timestamp_dict = {
                "timeStamp": datetime.datetime.utcnow(),
            }
            dict_item.update(timestamp_dict)
            self.football_matches_collection.insert_one(dict_item)
            return item

